import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable

fun main(args: Array<String>)
{
    LwjglApplication(MainPage(), config {
        width = 480
        height = 320
        useGL20 = true
        backgroundFPS = 30
        foregroundFPS = 120
        initialBackgroundColor = Color.GRAY
        resizable = false
        title = "ActorDemo"
    })
}

class MainPage : ApplicationAdapter()
{
    // 原始图像
    private val img by lazy { TextureRegion(Texture(Gdx.files.internal("mario.png"))) }
    // 切割原始图像
    private val imgs by lazy { img.split(64, 64) }
    // 切割翻转原始图像
    private val flipImgs by lazy {
        img.split(64, 64).apply {
            forEach {
                it.forEach {
                    it.flip(true, false)
                }
            }
        }
    }

    // 左边按钮
    private val leftBtn by lazy { ImageButton(TextureRegionDrawable(imgs[1][0]), TextureRegionDrawable(imgs[1][1])) }
    // 右边按钮
    private val rightBtn by lazy { ImageButton(TextureRegionDrawable(flipImgs[1][0]), TextureRegionDrawable(flipImgs[1][1])) }
    // 马里奥对象
    private val mario by lazy { Mario(0f, 50f, imgs, flipImgs) }
    // 背景图片
    private val image by lazy {
        image("bg.jpg").apply {
            height = Gdx.graphics.floatHeight
        }
    }

    private val stage by lazy { Stage() }

    override fun create()
    {

        rightBtn.setPosition(64f, 0f)

        leftBtn.setOnTouchListener { _, touchState ->
            when (touchState)
            {
                TouchState.touchDown ->
                {
                    mario.state = Mario.STATE.LEFT
                }
                TouchState.touchUp ->
                {
                    mario.state = Mario.STATE.LEFT_IDLE
                }
            }
        }
        rightBtn.setOnTouchListener { _, touchState ->
            when (touchState)
            {
                TouchState.touchDown ->
                {
                    mario.state = Mario.STATE.RIGHT
                }
                TouchState.touchUp ->
                {
                    mario.state = Mario.STATE.RIGHT_IDLE
                }
            }
        }

        stage.addActor(image)
        stage.addActor(leftBtn)
        stage.addActor(rightBtn)
        stage.addActor(mario)

        Gdx.input.inputProcessor = stage
    }

    override fun render()
    {
        
        clear()
        stage.act()
        stage.draw()
    }
}


class Mario(mX: Float, mY: Float, imgs: Array<out Array<out TextureRegion>>, flipImgs: Array<out Array<out TextureRegion>>) : Actor()
{
    private var stateTime = 0f
    private var animLeft: Animation
    private var animRight: Animation
    private var animLeftIdle: Animation
    private var animRightIdle: Animation
    var state: STATE
    private var frame: TextureRegion? = null

    init
    {
        x = mX
        y = mY
        state = STATE.RIGHT_IDLE
        animLeft = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(flipImgs[0][0])
            add(flipImgs[0][1])
            add(flipImgs[0][2])
        })
        animRight = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(imgs[0][0])
            add(imgs[0][1])
            add(imgs[0][2])
        })
        animLeftIdle = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(flipImgs[0][0])
        })
        animRightIdle = Animation(0.1f, com.badlogic.gdx.utils.Array<TextureRegion>().apply {
            add(imgs[0][0])
        })
    }

    private fun update()
    {
        stateTime += Gdx.graphics.deltaTime
        when (state)
        {
            STATE.LEFT ->
            { // 往左走，横坐标往左移动
                x -= 1.5f
                if (x <= 20)
                {
                    x = 20f
                }
                frame = animLeft.getKeyFrame(stateTime, true)
            }
            STATE.RIGHT ->
            {
                // 往右走，横坐标往右移动
                x += 1.5f

                if (x >= Gdx.graphics.floatWidth - (frame?.regionWidth ?: 0) - 20)
                {
                    x = Gdx.graphics.floatWidth - (frame?.regionWidth ?: 0) - 20
                }
                frame = animRight.getKeyFrame(stateTime, true)
            }
            STATE.LEFT_IDLE ->
            {
                frame = animLeftIdle.getKeyFrame(stateTime, true)
            }
            STATE.RIGHT_IDLE ->
            {
                frame = animRightIdle.getKeyFrame(stateTime, true)
            }
        }
    }

    override fun draw(batch: SpriteBatch, parentAlpha: Float)
    {
        update()
        frame?.let {
            batch.draw(it, x, y)
        }
        super.draw(batch, parentAlpha)
    }

    enum class STATE
    {
        /**
         * 往左走
         */
        LEFT,
        /**
         * 往右走
         */
        RIGHT,
        /**
         * 往左停
         */
        LEFT_IDLE,
        /**
         * 往右停
         */
        RIGHT_IDLE
    }
}
